package com.titar.robot.utils;

import android.app.Activity;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import com.titar.robot.base.BaseActivity;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import androidx.core.content.ContextCompat;


/**
 * Created by demo on 2017/4/10.
 */

public class PermissionUtils {
    /**
     * 检查单个权限
     * @param activity
     * @param permission
     * @param code
     * @return
     */
    public static boolean  checkPermission(BaseActivity activity, String permission, int code){
        return checkPermissions(activity,new String[]{permission},code);
    }

    /**
     * 申请没有的权限,已申请的会被过滤,
     * 如果没有过滤,会导致已申请的权限再次申请
     * 返回申请失败,不执行相应的方法
     * @param activity
     * @param permission
     * @param code
     * @return
     */
    public static boolean  checkPermissions(BaseActivity activity,String[] permission,int code){
        if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) {
            ArrayList<String> permissions=new ArrayList<>();
            for (String per :permission){
                if (ContextCompat.checkSelfPermission(activity,per)
                        != PackageManager.PERMISSION_GRANTED) {
                    permissions.add(per);
                }
            }
            if(permissions.size()>0){
                activity.requestPermissions(permissions.toArray(new String[permissions.size()]),code);
                return false;
            }

        }

        return  true;
    }
    public static boolean  checkPermissions2(Activity activity, String[] permission, int code){
        if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) {
            ArrayList<String> permissions=new ArrayList<>();
            for (String per :permission){
                if (ContextCompat.checkSelfPermission(activity,per)
                        != PackageManager.PERMISSION_GRANTED) {
                    permissions.add(per);
                }
            }
            if(permissions.size()>0){
                activity.requestPermissions(permissions.toArray(new String[permissions.size()]),code);
                return false;
            }
            Log.i("permission2","permission   "+true);
        }
        Log.i("permission2","permission   "+false);
        return  true;
    }

    //检查系统是否关闭app应用的通知权限
    public  static  boolean isNotificationEnabled(Context context) {

        String CHECK_OP_NO_THROW = "checkOpNoThrow";
        String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;

        Class appOpsClass = null;
        /* Context.APP_OPS_MANAGER */
        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());
            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE,
                    Integer.TYPE,String.class);
            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);

            int value = (Integer) opPostNotificationValue.get(Integer.class);
            return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) ==
                    AppOpsManager.MODE_ALLOWED);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }





}
